<head>
    <title>Product Types</title>
    <link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
    <div id="page">
        <div id="logo">
            <h1><a href="#">Product Types</a></h1>
        </div>
        <form onsubmit="handleSubmit(); return false;">
            <div id='infotab' name='infotab' style='display:none;'>
                <table>
                    <tr>
                        <td>
                            <input type='button' id='exitbutton' onclick='location.replace("index.html");' value='Exit' /><input type='button' id='searchbutton' onclick='ChangeTab(1);' value='Search' /><input type='button' id='infobutton' onclick='ChangeTab(0);' value='Info'>
                        </td>
                    </tr>
                </table>
                <table class='field'>
                    <tr>
                        <td class='field'>
                            TypeId:
                        </td>
                        <td class='field2'>
                            <input type='text' id='producttypeid' name='producttypeid'>
                        </td>
                    </tr>
                    <tr>
                        <td class='field'>
                            TypeName:
                        </td>
                        <td class='field2'>
                            <input type='text' id='typename' name='typename'>
                        </td>
                    </tr>
                    <tr>
                        <td class='field'>
                            TaxRate 1:
                        </td>
                        <td class='field2'>
                            <input type='text' id='taxrate1' name='taxrate1'>
                        </td>
                    </tr>
                    <tr>
                        <td class='field'>
                            TaxRate 2:
                        </td>
                        <td class='field2'>
                            <input type='text' id='taxrate2' name='taxrate2'>
                        </td>
                    </tr>
                </table>
                <div class='field'>
                    <input type='button' value='Save' onclick='Save();'/><input type='button' value='Delete' onclick='Delete(document.all.customerid.value);'/><input type='button' value='Exit' onclick='location.replace("index.html");'/>
                </div>
                <br/>
            </div>
            <div id='searchtab' style='display:none;'>
                <table class='field'>
                    <tr>
                        <td class='field'>
                            Search:
                        </td>
                        <td class='field2'>
                            <input type='text' id='searcht' name='searcht'>
                        </td>
                    </tr>
                </table>
                <div class='field'>
                    <input type='button' class='buttons' value='Search' onclick='Search(document.all.searcht.value,0);'/><input type='button' value='New' onclick='Load(0);'/><input type='button' value='Exit' onclick='location.replace("index.html");'/>
                </div><span id="status">&nbsp;</span>
            </div>
        </form>
        <!-- ====================================== -->
        <!-- End HTML code.  Begin JavaScript code. -->
        <script type="text/javascript" src="gears_init.js">
        </script>
        <script type="text/javascript" src="scripts.js">
        </script>
        <script>
            
            var db;
            var searchBookmark = 0;
            var isNew = true;
            init();
            var query = location.search;
            var itemid = query.substring(query.indexOf("id=") + 3, query.length);
            //alert(itemid);
            if (itemid != '') {
                var loadid = new Number(itemid); //converts to number to prevent injection
                Load(loadid);
                
            }
            else {
                document.all.producttypeid.value = '';
                Search('', 0);
            }
            
            
            function ChangeTab(item){
            
            
                if (item == 0) 
                    document.all.infotab.style.display = 'inline';
                else 
                    document.all.infotab.style.display = 'none';
                if (item == 1) //search
                    document.all.searchtab.style.display = 'inline';
                else 
                    document.all.searchtab.style.display = 'none';
                
            }
            
            // Open this page's local database.
            function init(option){
                var success = false;
                
                if (window.google && google.gears) {
                    try {
                        db = google.gears.factory.create('beta.database');
                        
                        if (db) {
                            db.open('psdb');
                            if (option) //reset db
                                db.execute('drop table ProductType');
                            db.execute('create table if not exists ProductType' +
                            ' (id text primarykey,' +
                            ' typename text,' +
                            ' taxrate1 real,' +
                            ' taxrate2 real' +
                            ')');
                            
                            success = true;
                            // Initialize the UI at startup.
                        
                        }
                        
                    } 
                    catch (ex) {
                        setError('Could not create database: ' + ex.message);
                        alert('Could not create database: ' + ex.message);
                    }
                }
                
                // Enable or disable UI elements
                
                var inputs = document.forms[0].elements;
                for (var i = 0, el; el = inputs[i]; i++) {
                    el.disabled = !success;
                }
                
            }
            
            
            function Delete(id){
                var rs = db.execute("select typename from ProductType where id='" + id + "'");
                
                if (confirm('Delete type: ' + rs.field(0) + '?') == true) {
                    db.execute("delete from ProductType where id='" + id + "'");
                    alert('Product Type deleted.');
                }
            }
            
            function Load(id){
            
                ChangeTab(0);
                
                var producttypeidT = document.all.producttypeid;
                producttypeidT.value = id;
                var typenameT = document.all.typename;
                var taxrate1T = document.all.taxrate1;
                var taxrate2T = document.all.taxrate2;
                
                if (id != '') {
                    var rs = db.execute("select * from ProductType where id='" + id + "'");
                    producttypeidT.value = id;
                    typenameT.value = rs.field(1);
                    taxrate1T.value = formatNumber(rs.field(2),false);
                    taxrate2T.value = formatNumber(rs.field(3),false);
                    rs.close();
                    isNew = false;
                }
                else {
                    producttypeidT.value = '';
                    typenameT.value = '';
                    taxrate1T.value = '0.00';
                    taxrate2T.value = '0.00';
                    isNew = true;
                }
            }
            
            function Save(){
                if (!google.gears.factory || !db) {
                    return;
                }
                var producttypeidT = document.all.producttypeid;
                var typenameT = document.all.typename;
                var taxrate1T = document.all.taxrate1;
                var taxrate2T = document.all.taxrate2;
                
                var producttypeid = producttypeidT.value;
                if (producttypeid == '') {
                    alert('TypeId is required');
                    return;
                }
                var typename = typenameT.value;
                if (typename == '') {
                    alert('TypeName is required');
                    return;
                }
                var taxrate1 = taxrate1T.value;
                var taxrate2 = taxrate2T.value;
                
                if (isNew == true) {
                    // Insert the new item.
                    //get new id
                    
                    // The Gears database automatically escapes/unescapes inserted values.
                    db.execute('insert into ProductType (id, typename, taxrate1, taxrate2) values (?,?,?,?)', [producttypeidT, typename, taxrate1, taxrate2]);
                    alert('Product Type added.');
                    
                }
                else {
                    db.execute('update ProductType Set typename=?, taxrate1=?, taxrate2=? Where id=?', [typename, taxrate1, taxrate2, producttypeid]);
                    alert('Product Type updated.');
                }
                isNew = false;
                
            }
            
            function Search(search, option){
                //alert('In search!');
                searchBookmark = searchBookmark + option;
                if (searchBookmark < 0) 
                    searchBookmark = 0;
                ChangeTab(1);
                var query = 'select * from ProductType where typename like "' + search + '%"';
                query += ' limit 10 offset ' + searchBookmark;
                
                var status = document.all.status;
                
                var html = '<div class="field" >Search Results:</div>';//'Searching...' ;
                var rs = db.execute(query);
                var count = 0;
                while (rs.isValidRow()) {
                
                
                    html += SearchResult4("'" + rs.field(0) + "'", rs.field(1), 'TaxA: ' + formatNumber(rs.field(2), false), 'TaxB: ' + formatNumber(rs.field(3), false));
                    count++;
                    rs.next();
                }
                if (searchBookmark >= 10) 
                    html += "<a href=# onclick='Search(\"" + search + "\",-10);'>[Back]</a>";
                if (count >= 10) 
                    html += "<a href=# onclick='Search(\"" + search + "\",10);'>[Next]</a>";
                
                status.innerHTML = html;
                
                rs.close();
            }
        </script>
        </body>
    </html>
